﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Syrinx
{
	public class SessionStateBizRules : Syrinx.BaseBizRules
	{
		public void updateSessionState(IApplication app, IRuleContext context, bool isNew)
		{
			//Code to setup SWAF session info in session state table
			using (MemoryStream ms = new MemoryStream())
			{
				IFormatter formatter = new BinaryFormatter();
				formatter.Serialize(ms, app.sessionData);
				FlexiMap m = new FlexiMap();
				m["sessionId"] = app.sessionId;
				m["userName"] = CurrentUser.get("userName");
				m["lastUpdated"] = DateTime.UtcNow;
				m["sessionState"] = ms.ToArray();
				if(isNew)
					app.appData.put("DA.Syrinx.AddSessionState", m);
				else
					app.appData.put("DA.Syrinx.UpdateSessionState", m);
			}
		}
	}
}
